package cn.changeforyou.web.blog.luceneAop;

import cn.changeforyou.web.blog.po.Article;
import com.ld.lucenex.base.BaseConfig;
import com.ld.lucenex.config.LuceneXConfig;
import com.ld.lucenex.core.LuceneX;
import com.ld.lucenex.service.LucenexServiceFactory;
import com.ld.lucenex.service.LucenexServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Dao;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.SetupBy;

/**
 * @author zhyu
 */
//@IocBean
//@Modules
//@SetupBy
@Slf4j
public class LuceneStarter/* implements Setup */{

	public LuceneX initLuceneX(LuceneConfig luceneConfig) {
		return new LuceneX(new LuceneXConfig() {
			@Override
			public void configLuceneX(BaseConfig me) {
				me.add(luceneConfig.getFilePath(), luceneConfig.getKey(), Article.class);
			}
		});
	}

	public LucenexServiceImpl initService(LuceneConfig luceneConfig) {
		return LucenexServiceFactory.getService(LucenexServiceImpl.class, luceneConfig.getKey());
	}

//	@Override
	public void init(NutConfig nc) {
		Ioc ioc = nc.getIoc();
		LuceneConfig luceneConfig = ioc.get(LuceneConfig.class);
		LuceneX luceneX = initLuceneX(luceneConfig);
		ioc.addBean("lucenexServiceImpl", initService(luceneConfig));
		ioc.addBean("luceneX", luceneX);

		Dao dao = ioc.get(Dao.class);
		log.info("lucene 配置完毕");
	}

//	@Override
	public void destroy(NutConfig nc) {

	}
}
